package logic

import (
	"context"
	"errors"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogic {
	return &UserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLogic) User(in *personrpc.UserRequest) (*personrpc.UserResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	err := user.FindUsers(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("当前账号不存在，请更换")
	}

	users := mysql.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		HeadImage: in.HeadImage,
		Name:      in.Name,
	}

	mysql.DB.Debug().Updates(&users)

	return &personrpc.UserResponse{
		UserId: int64(user.ID),
	}, nil
}
